package com.rc.evcharger.service.ugs;

import com.rc.evcharger.mapper.member.MemberAccountMapper;
import com.rc.evcharger.mapper.member.MemberInfoMapper;
import com.rc.evcharger.mapper.order.OrderInfoMapper;
import com.rc.evcharger.mapper.ugs.TenantUgsPointTypeMapper;
import com.rc.evcharger.mapper.ugs.UgsMemberPointRecordMapper;
import com.rc.evcharger.model.member.MemberAccount;
import com.rc.evcharger.model.member.MemberInfo;
import com.rc.evcharger.model.order.OrderInfo;
import com.rc.evcharger.model.ugs.TenantUgsPointType;
import com.rc.evcharger.model.ugs.TenantUgsPointTypeExample;
import com.rc.evcharger.model.ugs.UgsMemberPointRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * Create by Yang on 2021/7/21
 */
@Service
public class UgsMemberPointRecordServiceImpl implements UgsMemberPointRecordService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private UgsMemberPointRecordMapper ugsMemberPointRecordMapper;

    @Autowired
    private MemberAccountMapper memberAccountMapper;

    @Autowired
    private MemberInfoMapper memberInfoMapper;

    @Autowired
    private OrderInfoMapper orderInfoMapper;

    @Autowired
    private TenantUgsPointTypeMapper tenantUgsPointTypeMapper;

    @Override
    public UgsMemberPointRecordMapper getMapper() {
        return ugsMemberPointRecordMapper;
    }

    @Override
    public int insertUgsMemberPointRecord(Integer payAmount, Long memberId, Long orderId) {

        MemberAccount memberAccount = memberAccountMapper.selectByPrimaryKey(memberId);
        MemberInfo memberInfo = memberInfoMapper.selectByPrimaryKey(memberId);
        UgsMemberPointRecord ugsMemberPointRecord = new UgsMemberPointRecord();

        OrderInfo orderInfo = orderInfoMapper.selectByPrimaryKey(orderId);
        ugsMemberPointRecord.setOrderId(orderId);
        ugsMemberPointRecord.setOrderNum(orderInfo.getOrderNum());

        //查询积分生成规则  saas运营商共用同一个
        TenantUgsPointTypeExample tenantUgsPointTypeExample = new TenantUgsPointTypeExample();
        tenantUgsPointTypeExample.createCriteria().andPointTypeEqualTo("0").andStatusEqualTo("0");


        long point = 0;
        List<TenantUgsPointType> tenantUgsPointTypes = tenantUgsPointTypeMapper.selectByExample(tenantUgsPointTypeExample);
        if (CollectionUtils.isEmpty(tenantUgsPointTypes)) {
            //point = payAmount;
            logger.error("该运营商未设置积分消费规则,无法新增消费积分,对应运营商ID:{},订单号:{}", memberInfo.getTenantCode(), orderInfo.getOrderNum());
            return 1;
        } else {
            TenantUgsPointType tenantUgsPointType = tenantUgsPointTypes.get(0);

            //规则*实付金额=获得积分  否则返回实付金额整形数据
            point = payAmount * tenantUgsPointType.getPointAmountRatio();

            ugsMemberPointRecord.setPointTypeId(tenantUgsPointType.getPointTypeId());
            ugsMemberPointRecord.setRemark(tenantUgsPointType.getPointName());
        }


        MemberAccount newMemberInfo = new MemberAccount();
        newMemberInfo.setMemberId(memberAccount.getMemberId());
        newMemberInfo.setPoint((memberAccount.getPoint() + (int) point));
        //积分累加并保存
        memberAccountMapper.updateByPrimaryKeySelective(newMemberInfo);

        ugsMemberPointRecord.setTenantCode(memberAccount.getTenantCode());
        ugsMemberPointRecord.setMemberId(memberId);
        ugsMemberPointRecord.setMemberType(memberInfo.getMemberType());
        ugsMemberPointRecord.setRecordType("0");//记录类型  (0充电消费  1充值  2签到 3商品兑换 4.历史记录  5.分享  ) 默认0


        ugsMemberPointRecord.setPoint(point);
        ugsMemberPointRecord.setResult((long) newMemberInfo.getPoint());
        ugsMemberPointRecord.setCreatedTime(new Date());
        ugsMemberPointRecord.setLastUpdatedTime(new Date());

        //保存积分记录
        ugsMemberPointRecordMapper.insertSelective(ugsMemberPointRecord);

        return 0;
    }
}
